﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ReAnimation.MVP.ViewInterfaces;
using ReAnimation.MVP.Presenters.Helpers;
using ReAnimation.MVP.Presenters;
using ReAnimation.Helpers.Common;
using ReAnimation.MVP.Models.Interfaces;
using ReAnimation.Domain;

namespace ReAnimation.MVP.Views
{
    public partial class AnimationStepSettingsView : BaseUserControlView, IAnimationStepSettingsView
    {
        private IMoleculeRotationAndTranslateView _moleculeRotationAndTranslateView;
        private IMainModel _mainModel;
        private IZMatrixTable _zMatrixTable;
        private IMoleculeTransformMatrixView _molTransformMatrixView;
        private AnimationStepSettingsPresenter _presenter;
        private ICameraPositionSettingsView _cameraPositionSettingsView;

        public AnimationStepSettingsView(IMainModel mainModel,
            IMoleculeRotationAndTranslateView moleculeRotationAndTranslateView,
            ICameraPositionSettingsView cameraPositionSettingsView,
            IMoleculeTransformMatrixView molTransformMatrixView,
                                         IZMatrixTable zMatrixTable,
                                         AnimationStepSettingsPresenter presenter)
        {
            _moleculeRotationAndTranslateView = moleculeRotationAndTranslateView;
            _cameraPositionSettingsView = cameraPositionSettingsView;
            _mainModel = mainModel;
            _molTransformMatrixView = molTransformMatrixView;
            _zMatrixTable = zMatrixTable;
            _presenter = presenter;

            InternalInitializeComponent();

            _presenter.View = this;
            _mainModel.MoleculesCollectionChanged += (s,a)=>
            {
                currentMoleculeComboBox.DisplayMember = "DisplayName";
                currentMoleculeComboBox.ValueMember = "Value";
                currentMoleculeComboBox.DataSource = PrepareMoleculesListForComboBox();
                currentMoleculeComboBox.DisplayMember = "DisplayName";
                currentMoleculeComboBox.ValueMember = "Value";
            };
            _mainModel.CurrentMoleculeChanged += (s, a) => 
            {
                if (_mainModel.CurrentMolecule == null) return;
                currentMoleculeComboBox.SelectedValue = _mainModel.CurrentMolecule; 
            };
        }

        private IEnumerable<object> PrepareMoleculesListForComboBox()
        {
            if (_mainModel.CurrentAnimationStep == null)
                return new List<object>();
            return (from molecule in _mainModel.CurrentAnimationStep.Molecules
                    orderby molecule.Name
                    select new
                    {
                        DisplayName = molecule.Name,
                        Value = molecule
                    }).Cast<object>().ToList();
        }

        private void InternalInitializeComponent()
        {
            InitializeComponent();

            using (BatchLayoutChange.For(this))
            {
                var zMatrixTableControl = _zMatrixTable as UserControl;
                zMatrixTableControl.Dock = DockStyle.Fill;
                zMatrixTabPage.Controls.Add(zMatrixTableControl);

                var molTransformMatControl = _molTransformMatrixView as UserControl;
                molTransformMatControl.Dock = DockStyle.Fill;
                transformMatrixTabPage.Controls.Add(molTransformMatControl);

                var cameraPositionControl = _cameraPositionSettingsView as UserControl;
                cameraPositionControl.Dock = DockStyle.Fill;
                CameraSettingsTabPage.Controls.Add(cameraPositionControl);

                var moleculeRotAndTrans = _moleculeRotationAndTranslateView as UserControl;
                moleculeRotAndTrans.Dock = DockStyle.Fill;
                rotationAndTranslateTabPage.Controls.Add(moleculeRotAndTrans);
            }
        }

        public bool NewEnabled { set { btnNewMolecule.Enabled = value; } }
        public bool LoadEnabled { set { btnLoadMolecule.Enabled = value; } }
        public bool DelEnabled { set { btnDelMolecule.Enabled = value; } }
        public bool MoleculeDetailsEnabled { set { tabControl.Enabled = value; } }

        public event EventHandler NewMolecule;
        public event EventHandler LoadMolecule;
        public event EventHandler DeleteMolecule;

        private void btnNewMolecule_Click(object sender, EventArgs e)
        {
            NewMolecule.Throw(sender, e);
        }

        private void btnLoadMolecule_Click(object sender, EventArgs e)
        {
            LoadMolecule.Throw(sender,e);
        }

        private void btnDelMolecule_Click(object sender, EventArgs e)
        {
            DeleteMolecule.Throw(sender,e);
        }

        private void currentMoleculeComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            Molecule mol = currentMoleculeComboBox.SelectedValue as Molecule;
            if (_mainModel.CurrentAnimationStep != null &&
                _mainModel.CurrentAnimationStep.Molecules.Contains(mol))
            {
                _mainModel.CurrentMolecule = mol;
            }
            else
            {
                _mainModel.CurrentMolecule = null;
                
            }
        }
    }
}
